package com.ruoyi.project.system.user.controller;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.QRCodeUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.project.WebMoudle.ShopSuser.domain.ShopSuser;
import com.ruoyi.project.WebMoudle.ShopSuser.service.IShopSuserService;
import com.ruoyi.project.WebMoudle.WxShop.domain.WxShop;
import com.ruoyi.project.WebMoudle.WxShop.service.IWxShopService;
import com.ruoyi.project.WebMoudle.WxUser.service.IWxUserService;
import com.ruoyi.project.WebMoudle.streamShop.service.IStreamShopService;
import com.ruoyi.project.WebMoudle.streamTerrace.service.IStreamTerraceService;
import com.ruoyi.project.WebMoudle.useTerms.service.IUseTermsService;
import com.ruoyi.project.WebMoudle.wxOrder.service.IWxOrderService;
import org.aspectj.apache.bcel.generic.FieldOrMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.config.service.IConfigService;
import com.ruoyi.project.system.menu.domain.Menu;
import com.ruoyi.project.system.menu.service.IMenuService;
import com.ruoyi.project.system.user.domain.User;

/**
 * 首页 业务处理
 *
 * @author ruoyi
 */
@Controller
public class IndexController extends BaseController {
    @Autowired
    private IMenuService menuService;

    @Autowired
    private IConfigService configService;

    @Autowired
    private RuoYiConfig ruoYiConfig;
    @Autowired
    private IUseTermsService useTermsService;
    @Autowired
    private IWxShopService shopService;
    @Autowired
    private IWxUserService wxUserService;
    @Autowired
    private IShopSuserService shopSuserService;
    @Autowired
    private IWxOrderService wxOrderService;
    @Autowired
    private ServerConfig serverConfig;

    // 系统首页
    @GetMapping("/index")
    public String index(ModelMap mmap) {
        // 取身份信息
        User user = getSysUser();
        // 根据用户id取出菜单
        List<Menu> menus = menuService.selectMenusByUser(user);
        mmap.put("menus", menus);
        mmap.put("user", user);
        mmap.put("sideTheme", configService.selectConfigByKey("sys.index.sideTheme"));
        mmap.put("skinName", configService.selectConfigByKey("sys.index.skinName"));
        mmap.put("copyrightYear", ruoYiConfig.getCopyrightYear());
        mmap.put("demoEnabled", ruoYiConfig.isDemoEnabled());
        return "index";
    }

    // 切换主题
    @GetMapping("/system/switchSkin")
    public String switchSkin(ModelMap mmap) {
        return "skin";
    }


    // 系统介绍
    @GetMapping("/system/main")
    public String main(ModelMap mmap) {
        ShopSuser shopSuser = shopSuserService.selectOneShopId(getUserId());
        if (StringUtils.isNotNull(shopSuser)) {     //商铺登录
            WxShop wxShop = shopService.selectWxShopById(shopSuser.getShopId());
            mmap.put("shopMoney", wxShop.getShopMoney());
            mmap.put("vipCount", wxUserService.selectVipCount(shopSuser.getShopId()));
            mmap.put("orderCount", wxOrderService.queryShopOrderCount(shopSuser.getShopId()));
            mmap.put("empCount", shopSuserService.queryEmpCount(shopSuser.getShopId()) - 1);
            return "shopShow";
        }
        // 平台登录
        mmap.put("$terrace", useTermsService.selectUseTermsList(null).get(0).getTerraceMoney());
        mmap.put("$shop", shopService.queryShopMoneySum()==null?0:shopService.queryShopMoneySum());
        mmap.put("$user", wxUserService.queryUserMoneySum()==null?0:wxUserService.queryUserMoneySum());
        mmap.put("grade", wxUserService.queryUserGradeSum()==null?0:wxUserService.queryUserGradeSum());
        return "terraceShow";
    }
}
